package cn.jeeweb.modules.web.statistics.service.impl;

import cn.jeeweb.modules.sys.entity.Dict;
import cn.jeeweb.modules.sys.mapper.DictMapper;
import cn.jeeweb.modules.sys.utils.DictUtils;
import cn.jeeweb.modules.web.labor.mapper.HuEmployMentMapper;
import cn.jeeweb.modules.web.labor.mapper.HuLaborMapper;
import cn.jeeweb.modules.web.labor.mapper.HuTrainNoticeMapper;
import cn.jeeweb.modules.web.labor.service.IHuEmployMentService;
import cn.jeeweb.modules.web.labor.service.IHuLaborService;
import cn.jeeweb.modules.web.statistics.data.EchartsData;
import cn.jeeweb.modules.web.statistics.service.ILaborDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service("laborData")
public class LaborDataService implements ILaborDataService {
    @Autowired
    private HuLaborMapper huLaborMapper;
    @Autowired
    private HuEmployMentMapper huEmployMentMapper;
    @Autowired
    private HuTrainNoticeMapper huTrainNoticeMapper;
    @Override
    public void largeLaborData(HttpServletRequest request) {
        //劳动力总数
        Integer ldlzs = huLaborMapper.selectCount();
        request.setAttribute("ldlzs", ldlzs);
        //贫困户总数
        Integer pkh = huEmployMentMapper.selectByPoor();
        request.setAttribute("pkh", pkh);

        //start 培训通知下发按月统计
        Map<Integer, Integer> pxtzxfData = new HashMap<>();
        List<Map<String, Object>> pxtzxf = huTrainNoticeMapper.selectCountByCreateDate();
        String xAxisData = "";
        String seriesData = "";
        boolean temp = false;
        for (int i = 1; i < 13; i++) {
            for (int j = 0; j < pxtzxf.size(); j++) {
                if(i == Integer.parseInt(pxtzxf.get(j).get("m").toString())){
                    xAxisData = xAxisData + i + ",";
                    seriesData = seriesData + pxtzxf.get(j).get("c") + ",";
                    temp = true;
                    break;
                }
            }
            if(temp){
                temp = false;
            }else{
                xAxisData = xAxisData + i + ",";
                seriesData = seriesData + 0 + ",";
            }
        }
        request.setAttribute("barData", EchartsData.eData("bar","["+xAxisData+"]","["+ seriesData+"]", "培训通知下发情况"));
        //end 培训通知下发按月统计

        //start 按照劳动力文化程度统计
        List<Map<String, String>> whcdList = huLaborMapper.selectCountByEducation();
        List<Dict> dicts = DictUtils.getDictList("education");
        String whcdDataAxis = "";
        String whcdData = "";
        boolean whcdfbTemp = false;
        for (int i = 0; i < dicts.size(); i++) {
            for (int j = 0; j < whcdList.size(); j++) {
                if(dicts.get(i).getValue().equals(whcdList.get(j).get("education"))){
                    whcdDataAxis = whcdDataAxis + "'" + dicts.get(i).getLabel() + "',";
                    whcdData = whcdData + whcdList.get(j).get("c") + ",";
                    whcdfbTemp = true;
                    break;
                }
            }
            if(whcdfbTemp){
                whcdfbTemp = false;
            }else{
                whcdDataAxis = whcdDataAxis + "'" + dicts.get(i).getLabel() + "',";
                whcdData = whcdData + 0 + ",";
            }
        }
        request.setAttribute("whcdDataAxis", "[" + whcdDataAxis + "]");
        request.setAttribute("whcdData", "[" + whcdData + "]");
        //end 按照劳动力文化程度统计

        //start 按照劳动力性别统计
        List<Map<String, String>> xbList = huLaborMapper.selectCountBySex();
        if(xbList == null || xbList.size() == 0){
            request.setAttribute("nv", 0);
            request.setAttribute("nan", 0);
        }else if(xbList.size() == 1){
            if("0".equals(xbList.get(0).get("sex"))){
                request.setAttribute("nv", xbList.get(0).get("c"));
                request.setAttribute("nan", 0);
            }else{
                request.setAttribute("nv", 0);
                request.setAttribute("nan", xbList.get(0).get("c"));
            }
        }
        //end 按照劳动力性别统计

        request.setAttribute("whcdfb", EchartsData.whcdfbData(null,null,null,null));
    }
}
